package com.liuwa.knowledge.controller;

import java.util.List;

import com.liuwa.common.utils.bean.BeanUtils;
import com.liuwa.common.exception.ServiceException;
import com.liuwa.common.utils.poi.ExportResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.liuwa.common.annotation.Log;
import com.liuwa.common.core.controller.BaseController;
import com.liuwa.common.enums.BusinessType;
import com.liuwa.knowledge.domain.Book;
import com.liuwa.knowledge.query.BookQuery;

import com.liuwa.knowledge.service.BookService;
import com.liuwa.common.utils.poi.ExcelUtil;
import com.liuwa.common.bean.Page;

/**
 * 教材Controller
 * 
 * @author liuwa
 * @date 2025-04-02 08:46:14
 */
@RestController
@Api(value = "教材")
@RequestMapping("/knowledge/books")
public class BookController extends BaseController{

    @Autowired
    private BookService bookService;

    /**
     * 查询教材列表
     */
    @ApiOperation("查询教材列表")
    @PreAuthorize("@ss.hasPermi('knowledge:book:list')")
    @GetMapping
    public Page<Book> list(BookQuery query)
    {
        Book condition = new Book();
        BeanUtils.copyBeanNotNull2Bean(query, condition);
        startPage(query);
        List<Book> list = bookService.findList(condition);
        return getPage(list, query);
    }


    /**
     * 导出教材列表
     */
    @ApiOperation("导出教材")
    @PreAuthorize("@ss.hasPermi('knowledge:book:export')")
    @Log(title = "导出教材", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ExportResult export(BookQuery query)
    {
        Book condition = new Book();
        BeanUtils.copyBeanNotNull2Bean(query, condition);
        List<Book> list = bookService.findList(condition);
        ExcelUtil<Book> util = new ExcelUtil<Book>(Book.class);
        return util.exportExcel(list, "教材数据");
    }

    /**
     * 获取教材详细信息
     */
    @ApiOperation("获取教材详细信息")
    @PreAuthorize("@ss.hasPermi('knowledge:book:query')")
    @GetMapping(value = "/{id}")
    public Book get(@PathVariable("id") Integer id)
    {
        return bookService.get(id);
    }

    /**
     * 新增教材
     */
    @ApiOperation("新增教材")
    @PreAuthorize("@ss.hasPermi('knowledge:book:add')")
    @Log(title = "新增教材", businessType = BusinessType.INSERT)
    @PostMapping
    public void add(@RequestBody Book book)
    {
        bookService.insert(book);
    }

    /**
     * 修改教材
     */
    @ApiOperation("修改教材")
    @PreAuthorize("@ss.hasPermi('knowledge:book:edit')")
    @Log(title = "修改教材", businessType = BusinessType.UPDATE)
    @PutMapping
    public void edit(@RequestBody Book book)
    {
        Book oldBook = bookService.get(book.getId());
        if(oldBook == null){
            throw new ServiceException("操作的教材不存在或已删除");
        }
        BeanUtils.copyBeanNotNull2Bean(book, oldBook);
        bookService.update(oldBook);
    }



    /**
     * 删除教材
     */
    @ApiOperation("删除教材")
    @PreAuthorize("@ss.hasPermi('knowledge:book:remove')")
    @Log(title = "删除教材", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public void remove(@PathVariable Integer[] ids)
    {
		bookService.batchDeleteByLogic(ids);
    }
	
	
	
    /**
     * 查询教材已删除列表
     */
    @ApiOperation("查询教材已删除列表")
    @PreAuthorize("@ss.hasPermi('knowledge:book:trash')")
    @GetMapping("/deleted")
    public Page<Book> deleted(BookQuery query)
    {
        Book condition = new Book();
        BeanUtils.copyBeanNotNull2Bean(query, condition);
        startPage();
        List<Book> list = bookService.findDeletedList(condition);
        return getPage(list, query);
    }



    /**
     * 恢复教材
     */
    @ApiOperation("恢复教材")
    @PreAuthorize("@ss.hasPermi('knowledge:book:restore')")
    @Log(title = "恢复教材", businessType = BusinessType.UPDATE)
    @PutMapping("/{ids}/restore")
    public void restore(@PathVariable Integer[] ids)
    {
        bookService.batchRestore(ids);
    }
}
